import time
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .verification import main
import os

# cur_path = os.path.abspath(os.path.dirname(__file__))
# rootPath = cur_path[:cur_path.find("baseline-autotest\\") + len("baseline-autotest\\")]
# png_path = rootPath + "baseline_autotest\\cases\\lib\\img\\pic.png"
# png_path1 = rootPath + "baseline_autotest\\cases\\lib\\img\\pic1.png"


def login_test(username, driver, new_password):

    # 不安全
    # driver.find_element_by_id("details-button").click()
    # time.sleep(1)
    # driver.find_element_by_id("proceed-link").click()
    # time.sleep(1)

    # 登录
    driver.find_element_by_css_selector("input[type='text']").send_keys(username)
    time.sleep(1)
    print("---------1111111111")
    print(new_password)
    driver.find_element_by_css_selector("input[type='password']").send_keys(new_password)
    time.sleep(1)

    # 验证码
    # driver.save_screenshot(png_path)
    # code_ele = driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[3]/div/span/img')
    # print("验证码的坐标为：", code_ele.location)
    # print("验证码的大小为：", code_ele.size)
    #
    # left = code_ele.location["x"]
    # top = code_ele.location["y"]
    # right = code_ele.size["width"] + left
    # down = code_ele.size["height"] + top
    # image = Image.open(png_path)
    #
    # code_image = image.crop((left, top, right, down))
    # code_image.save(png_path1)
    #
    # result = main()
    # a = main()
    # b = a[0]["words"]
    # print(type(b))

    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[3]/div/div[1]/input').send_keys("8888")
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[1]/form/div[4]/div/button/span').click()
    time.sleep(1)
    try:
        text = driver.find_element_by_xpath('/html/body/div[2]/p').text
        print(text)
    except NoSuchElementException:
        pass

    return driver
